package com.example.administrator.androidros.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.androidros.R;
import com.example.administrator.androidros.activity.PermissionManageActivity;
import com.example.administrator.androidros.adapter.MessageAdapter;
import com.example.administrator.androidros.base.BaseFragment;
import com.example.administrator.androidros.event.MessageEvent;
import com.example.administrator.androidros.model.MessageModel;
import com.example.administrator.androidros.net.HttpUtils;
import com.example.administrator.androidros.utils.AgentConstants;
import com.example.administrator.androidros.utils.AgentSharedPreferences;
import com.example.administrator.androidros.utils.DisplayHelper;
import com.example.administrator.androidros.utils.ServerConstants;
import com.example.administrator.androidros.widget.CustomPopupMenu;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huhaichao on 2018/3/12.
 */

public class MessageFragment extends BaseFragment {
    @BindView(R.id.fragment_message_list)
    protected RecyclerView fragment_message_list;

    @BindView(R.id.fragment_message_refreshLayout)
    protected SmartRefreshLayout fragment_message_refreshLayout;

    private MessageAdapter messageAdapter;
    private List<MessageModel> data;
    private List<Integer> moreIconList = new ArrayList<>();//更多
    private List<String> moreTitleList = new ArrayList<>();

    public static MessageFragment newInstance() {
        return new MessageFragment();
    }

    @Override
    protected void initData(View view, Bundle savedInstanceState) {
        setStatusBar();
        initMoreData();
        initScene();
        initRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        fragment_message_refreshLayout.finishRefresh();
        if (object != null) {
            switch (requestCode) {
                case 1001:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                        getMessageList();
                    } else {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
                case 1002:
                    MessageModel[] deviceModels = (MessageModel[]) HttpUtils.getData(object, MessageModel[].class);
                    if (deviceModels != null) {
                        data = Arrays.asList(deviceModels);
                        messageAdapter.setNewData(data);
                    }
                    EventBus.getDefault().post(new MessageEvent(data.size()));
                    break;
                default:
                    break;
            }
        }
    }

    protected void setStatusBar() {
        //设置状态栏颜色和位移一段距离
        DisplayHelper.setStatusBarColor(mBaseActivity, R.color.common_head);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) fragment_message_refreshLayout.getLayoutParams();
        lp.setMargins(0, DisplayHelper.getStatusBarHeight(mBaseActivity), 0, 0);
    }

    protected void initScene() {
        data = new ArrayList<>();
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mBaseActivity, LinearLayoutManager.VERTICAL, false);
        fragment_message_list.setLayoutManager(mLayoutManager);
        messageAdapter = new MessageAdapter(R.layout.message_item, data);
        messageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                final CustomPopupMenu.Builder builder = new CustomPopupMenu.Builder(mBaseActivity);
                builder.setIcon(moreIconList)
                        .setTitle(moreTitleList)
                        .isShowLine(true)
                        .isShowIcon(false)
                        .setItemLayoutParams(-2, -2)
                        .setItemLayoutPadding(10, 10, 20, 20)
                        .setContentView(view)
                        .setOff(-(int) (view.getWidth() / DisplayHelper.DENSITY / 2.5), 0)
                        .setOnMenuItemClickListener(new CustomPopupMenu.onMenuItemClickListener() {
                            @Override
                            public void onMenuItemClick(int itemPosition) {
                                //忽略：messageRead：1  messageStatus:2
                                updateMessageRead(messageAdapter.getData().get(position).getMasterId(),messageAdapter.getData().get(position).getMessageType());
                                builder.dismiss();
                            }
                        })
                        .create()
                        .show();
            }
        });
        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startOtherActivity(PermissionManageActivity.class, null);
            }
        });

        fragment_message_list.setAdapter(messageAdapter);
    }

    protected void getMessageList() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberId", AgentSharedPreferences.getMemberInfo(mBaseActivity).getMemberTelephone());
        jsonObject.put("messageType", "20");
        jsonObject.put("messageRead", "0");
        jsonObject.put("messageStatus", "0");
        HttpUtils.post(mBaseActivity, 1002, ServerConstants.QUERY_MESSAGE, jsonObject.toString(), this);//主要回调的类
//        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_loading), true);
    }

    //messageStatus:3   忽略
    protected void updateMessageRead(String masterId,String messageType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("masterId", masterId);
        jsonObject.put("messageType",messageType);
        jsonObject.put("messageRead", "1");
        jsonObject.put("messageStatus", "3");
        HttpUtils.post(mBaseActivity, 1001, ServerConstants.UPDATE_MESSAGE, jsonObject.toString(), this);//主要回调的类
    }

    @Override
    public void onResume() {
        super.onResume();
        getMessageList();
    }

    public void initMoreData() {
        moreIconList.clear();
        moreIconList.add(R.mipmap.icon_homepage_select);
        moreTitleList.clear();
        moreTitleList.add("忽略");
    }

    public void initRefresh() {
        fragment_message_refreshLayout.setEnableLoadmore(false);
        fragment_message_refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getMessageList();
            }
        });
    }
}
